// src/utils/event.js

const events = {}

function on(eventName, callback) {
    if (!events[eventName]) {
        events[eventName] = []
    }
    events[eventName].push(callback)
}

function emit(eventName, data) {
    if (events[eventName]) {
        events[eventName].forEach(callback => callback(data))
    }
}

function off(eventName, callback) {
    if (!callback) {
        delete events[eventName]
    } else {
        const index = events[eventName]?.indexOf(callback)
        if (index > -1) {
            events[eventName].splice(index, 1)
        }
    }
}

export default { on, emit, off }